package com.icow.wms.basic.domain;

import com.icow.wms.basic.utils.DoorHandler;
import com.icow.wms.basic.utils.OpenTcs;
import com.icow.wms.basic.utils.Pipe;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicBoolean;

@Data
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings("all")
@Slf4j
public class Door implements Runnable {

    private boolean doorUpDI1 = false;
    private boolean doorUpDI2 = false;
    private boolean doorUpDI3 = false;
    private boolean doorUpDI4 = false;
    private String pipe;
    private String vehicleName;

    private String ip;
    private String closeDoorPosition;

    public Door(String ip) {
        this.ip = ip;
    }

    private AtomicBoolean accessed = new AtomicBoolean(false);

    public void clearDoorState() {
        if (!accessed.getAndSet(true)) {
            doorUpDI1 = false;
            doorUpDI2 = false;
            doorUpDI3 = false;
            doorUpDI4 = false;
        }
    }

    public void canClearDoorState() {
        accessed.set(false);
        doorUpDI1 = false;
        doorUpDI2 = false;
        doorUpDI3 = false;
        doorUpDI4 = false;
    }

    public boolean openDoor(String ip, String openDoorPipe) {
        byte[] openDoorBytes = DoorHandler.getCmdMsgForDoor(openDoorPipe, true);
        boolean openDoor = DoorHandler.sendMsg(ip, openDoorBytes);
        return openDoor;
    }

    public boolean closeDoor(String ip, String openDoorPipe) {
        byte[] openDoorBytes = DoorHandler.getCmdMsgForDoor(openDoorPipe, false);
        boolean openDoor = DoorHandler.sendMsg(ip, openDoorBytes);
        return openDoor;
    }

    public boolean getDoorState(String ip, String doorUpPipe) {
        byte[] readDoorBytes = DoorHandler.getReadMsgForDoor();
        boolean openDoor = DoorHandler.sendMsg(ip, readDoorBytes);
        if (!openDoor) {
            log.info("read door state error");
            return false;
        }
        if (Pipe.DI1.equals(doorUpPipe)) {
            return doorUpDI1;
        } else if (Pipe.DI2.equals(doorUpPipe)) {
            return doorUpDI2;
        } else if (Pipe.DI3.equals(doorUpPipe)) {
            return doorUpDI3;
        } else if (Pipe.DI4.equals(doorUpPipe)) {
            return doorUpDI4;
        }
        return false;
    }

    @Override
    public void run() {
        int doorCase = 1;
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (ip == null) {
                log.info("ip is null");
                return;
            }
            switch (doorCase) {
                case 1:
                    String vehiclePosition = OpenTcs.getVehiclePosition(vehicleName);
                    boolean openDoor = openDoor(ip, pipe);
                    if (openDoor && closeDoorPosition.equals(vehiclePosition)) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (closeDoor(ip, pipe)) {
                            log.info(vehicleName + " arrive closeDoorPosition " + closeDoorPosition + " closeDoor");
                            return;
                        }
                    }
                    break;
            }
        }
    }
}
